using HotChocolate.Data.MongoDb.Sorting;
using HotChocolate.Data.Sorting;

namespace HotChocolate.Data;

public static class SortConventionDescriptorMongoExtensions
{
    /// <summary>
    /// Adds a <see cref="MongoDbSortProvider"/> with default configuration
    /// </summary>
    /// <param name="descriptor">The descriptor where the provider is registered</param>
    /// <returns>The <paramref name="descriptor"/> that was passed in as a parameter</returns>
    public static ISortConventionDescriptor UseMongoDbProvider(
        this ISortConventionDescriptor descriptor) =>
        descriptor.Provider(new MongoDbSortProvider(x => x.AddDefaultFieldHandlers()));

    /// <summary>
    /// Initializes the default configuration of the provider by registering handlers
    /// </summary>
    /// <param name="descriptor">The descriptor where the handlers are registered</param>
    /// <returns>The <paramref name="descriptor"/> that was passed in as a parameter</returns>
    public static ISortProviderDescriptor<MongoDbSortVisitorContext> AddDefaultFieldHandlers(
        this ISortProviderDescriptor<MongoDbSortVisitorContext> descriptor)
    {
        descriptor.AddOperationHandler(MongoDbAscendingSortOperationHandler.Create);
        descriptor.AddOperationHandler(MongoDbDescendingSortOperationHandler.Create);
        descriptor.AddFieldHandler(MongoDbDefaultSortFieldHandler.Create);
        return descriptor;
    }
}
